package com.paipai.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paipai.client.UserClient;
import com.paipai.entity.User;
import com.paipai.jwtproperty.JwtProperties;
import com.paipai.utils.JwtUtils;
import com.paipai.utils.UserInfo;
@Service
public class AuthService {
	
	@Autowired
	private UserClient userClient ;
	
	
	@Autowired
	private JwtProperties jwp;

	public String authentication(User user) {
		
		try {
			//远程调用
			User loginUser =userClient.UserLogin(user);
			//null即为用户不存在
			if(loginUser==null) {
				return null;
			}
			//提取用户的id,用户名
			UserInfo userInfo = new UserInfo();
			userInfo.setId(Long.parseLong(loginUser.getId().toString()));
			userInfo.setUsername(loginUser.getUname());
			//根据userInfo 生成token
			String token = JwtUtils.generateToken(userInfo,  jwp.getPrivateKey(), jwp.getExpire());
			return token;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
		
		
	}

}
